package my.jieh.tips.util.timer;

/**
 * <p>Title: Core Java Technologies Tech Tips</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: Blueimp</p>
 * @author jieh
 * @version 1.0
 */

import java.util.*;

public class AnnoyingBeep {
  java.awt.Toolkit toolkit;
  Timer timer;

  public AnnoyingBeep() {
    toolkit = java.awt.Toolkit.getDefaultToolkit();
    timer = new Timer();
    timer.schedule(new RemindTask(),
                   0, //initial delay
                   1 * 1000); //subsequent rate
  }

  class RemindTask
      extends TimerTask {
    int numWarningBeeps = 3;

    public void run() {
      if (numWarningBeeps > 0) {
        toolkit.beep();
        System.out.println("Beep!");
        numWarningBeeps--;
      }
      else {
        toolkit.beep();
        System.out.println("Time's up!");
        //timer.cancel(); //Not necessary because we call System.exit
        System.exit(0); //Stops the AWT thread (and everything else)
      }
    }
  }

  public static void main(String args[]) {
    new AnnoyingBeep();
  }
}